package com.epro.dx.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.util.DialogUtils;
import com.epro.dx.util.IntentUtils;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.weipu.common.DXApplication;
import com.weipu.common.DXApplication.TrackerName;
import com.weipu.common.constants.Constant;
import com.weipu.common.util.Common;
import com.weipu.common.util.utils.SPUtils;

import kotlin.KotlinVersion;

/**
 * About Us
 *
 * @author hjd008
 * @version [版本号, 2015-2-10]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class AboutUsActivity extends BaseFragmentActivity {

    private TextView versionTv;

    private ImageView versionImg;

    private RelativeLayout curVersionLayout;

//    private RelativeLayout feedBackLayout;

    private RelativeLayout ratingLayout;
    private TextView curVersionTv;
    private String newVersionCode;
    private DialogUtils dialogUtils;
    private boolean canUpdate;
    private SPUtils spUtils;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取从上个页面传过来的 最新的版本号
        newVersionCode = IntentUtils.getString(getIntent(), Constant.Intent.INTENT_PUT_VERSIONCODE, Common.getVersionName());

        Tracker tracker = DXApplication.getContext().getTracker(TrackerName.APP_TRACKER);
        tracker.setScreenName(AboutUsActivity.class.getSimpleName());
        tracker.send(new HitBuilders.ScreenViewBuilder().build());
        //设置窗体无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_aboutus);
        setLeftBtnContent(true, -1);
        setTitleContent(R.string.settings_aboutus);
        //setLeftBtnToHome(HomeActivity.titleArray[3]);

        curVersionTv = (TextView) findViewById(R.id.aboutus_current_version);
        curVersionLayout = (RelativeLayout) findViewById(R.id.aboutus_layout_new_version);
//        curVersionLayout.setEnabled(false);
        versionTv = (TextView) findViewById(R.id.aboutus_tv_version);
        versionImg = (ImageView) findViewById(R.id.aboutus_img_version);

//        feedBackLayout = (RelativeLayout)findViewById(R.id.aboutus_layout_feedback);
        ratingLayout = (RelativeLayout) findViewById(R.id.aboutus_layout_rating);

        curVersionTv.setText(Common.getVersionName());
        spUtils = new SPUtils(getResources().getString(R.string.app_name_en));

        initEvent();
        checkUpdateCancelable();
        //友盟的软件更新
//        curVersionLayout.setOnClickListener(new OnClickListener()
//        {
//            @Override
//            public void onClick(View v)
//            {
//                UmengUpdateAgent.setUpdateAutoPopup(true);
//              //表示非增量更新 增量更新支持需要引入libbspatch.so文件
//				//http://dev.umeng.com/auto-update/android-doc/quick-start
//		        UmengUpdateAgent.setDeltaUpdate(false);
//                UmengUpdateAgent.forceUpdate(AboutUsActivity.this);
//            }
//        });
//        UmengUpdateAgent.setUpdateAutoPopup(false);
//        UmengUpdateAgent.setUpdateListener(new UmengUpdateListener()
//        {
//            @Override
//            public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo)
//            {
//                switch (updateStatus)
//                {
//                    case UpdateStatus.Yes:
//                        versionTv.setVisibility(View.INVISIBLE);
//                        versionImg.setVisibility(View.VISIBLE);
//                        curVersionLayout.setEnabled(true);
//                        break;
//                    case UpdateStatus.No:
//                        versionTv.setVisibility(View.VISIBLE);
//                        versionImg.setVisibility(View.INVISIBLE);
//                        curVersionLayout.setEnabled(false);
//                        break;
//                }
//            }
//        });
//        UmengUpdateAgent.update(AboutUsActivity.this);

//        //反馈
//        feedBackLayout.setOnClickListener(new OnClickListener()
//        {
//            @Override
//            public void onClick(View v)
//            {
//                //                FeedbackAgent agent = new FeedbackAgent(AboutUsActivity.this);
//                //                agent.startFeedbackActivity();
//                startActivity(new Intent(AboutUsActivity.this, FeedBackActivity.class));
//            }
//        });

    }

    private void initEvent() {
        ratingLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpGoogleMarket();
            }
        });

        //New Version 请求新版本
        curVersionLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (canUpdate) {

                    final View dialogView = LayoutInflater.from(AboutUsActivity.this).inflate(R.layout.dialog_update, null);
                    dialogUtils = new DialogUtils.Builder(AboutUsActivity.this)
                            .setContentView(dialogView)
                            .setEtContent(spUtils.getString(Constant.Global.APP_UPDATE_CONTENT))
                            .setViewClick(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    switch (v.getId()) {
                                        case R.id.btn_yes:
                                            jumpGoogleMarket();
                                            break;

                                        case R.id.btn_remind_no_more:
                                            //以后都不在显示这个更新对话框,即将显示过的标记放入SharedPreferences当中存储
                                            spUtils.put(Constant.Global.IS_SHOW_UPDATE_DIALOG, true);
                                            break;

                                        case R.id.btn_remind_later:

                                        default:
                                            break;
                                    }
                                    //对话框消失
                                    dialogUtils.dismiss();
                                }
                            }, R.id.btn_yes, R.id.btn_remind_later, R.id.btn_remind_no_more)
                            .build();
                    dialogUtils.showDialog();
                }

            }
        });
    }

    /**
     * 跳转到谷歌电子市场，如果用户有装Google Play的时候
     * 如果没有装Google Play，则会用手机自带的浏览器打开Google Play的网页
     */
    private void jumpGoogleMarket() {
        try {
                Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.vending");
            if (null == launchIntent) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName())));
            } else {
                ComponentName comp =
                        new ComponentName("com.android.vending",
                                "com.google.android.finsky.activities.LaunchUrlHandlerActivity");
                launchIntent.setComponent(comp);
                launchIntent.setData(Uri.parse("market://details?id=" + getPackageName()));
                startActivity(launchIntent);
            }
        } catch (android.content.ActivityNotFoundException anfe) {
            startActivity(new Intent(Intent.ACTION_VIEW,
                    Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName())));
        }
    }

    /**
     * 检查是否能够更新
     */
    private void checkUpdateCancelable() {
        if (!TextUtils.equals(newVersionCode, String.valueOf(Common.getVersionName()))) {
            canUpdate = true;
            //可以更新
            versionTv.setText(spUtils.getString(Constant.Global.APP_VERSION_CODE,Common.getVersionName()));
            versionTv.setTextColor(getResources().getColor(R.color.red));
            curVersionLayout.setEnabled(true);
            curVersionLayout.setVisibility(View.VISIBLE);

        } else {
            //不可以更新
            versionTv.setText(getResources().getString(R.string.cannot_update));
            curVersionLayout.setEnabled(false);
            curVersionLayout.setVisibility(View.GONE);
        }
    }
}
